#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <list>
#include "../../../NetEngine_Linux/NetEngine_SourceCode/XyRyNet_CommHdr.h"
#include "../../../NetEngine_Linux/NetEngine_SourceCode/XyRyNet_Types.h"
#include "../../../NetEngine_Linux/NetEngine_SourceCode/XyRyNet_ProtocolHdr.h"
#include "../../../NetEngine_Linux/NetEngine_SourceCode/NetEngine_StreamMedia/StreamMedia_HLSProtocol/HLSProtocol_Define.h"
#include "../../../NetEngine_Linux/NetEngine_SourceCode/NetEngine_StreamMedia/StreamMedia_HLSProtocol/HLSProtocol_Error.h"

//g++ -std=gnu++11 -Wall -g Test_HLSProtocol.cpp -o Test_HLSProtocol -L ../../../NetEngine_Linux/NetEngine_Release/NetEngine_Release/NetEngine_BaseLib -L ../../../NetEngine_Linux/NetEngine_Release/NetEngine_Release/NetEngine_Core -L ../../../NetEngine_Linux/NetEngine_Release/NetEngine_Release/NetEngine_StreamMedia -L ../../../NetEngine_Linux/NetEngine_Release/NetEngine_Release/NetEngine_SystemSdk -lNetEngine_BaseLib -lNetEngine_Core -lNetEngine_ManagePool -lStreamMedia_HLSProtocol -lNetEngine_SystemApi -lNetEngine_ProcSdk -Wl,-rpath=../../../NetEngine_Linux/NetEngine_Release/NetEngine_Release/NetEngine_BaseLib:../../../NetEngine_Linux/NetEngine_Release/NetEngine_Release/NetEngine_Core:../../../NetEngine_Linux/NetEngine_Release/NetEngine_Release/NetEngine_StreamMedia:../../../NetEngine_Linux/NetEngine_Release/NetEngine_Release/NetEngine_SystemSdk,--disable-new-dtags

XNETHANDLE xhRoot;
XNETHANDLE xhLow;
XNETHANDLE xhNormal;
XNETHANDLE xhHigh;

void __stdcall HLSProtocol_CBNotify(LPCTSTR lpszFileName, double dlTime, int nIndex, LPVOID lParam)
{
    printf("File:%s,Index:%d,Time:%lf\n", lpszFileName, nIndex, dlTime);
    HLSProtocol_M3u8File_AddFile(xhRoot, xhNormal, lpszFileName, dlTime, FALSE);
}

int main()
{
    XNETHANDLE xhToken = 0;

    LPCTSTR lpszVideoFile = _T("./480p.264");
    LPCTSTR lpszAudioFile = _T("./test.aac");
    LPCTSTR lpszDstFile = _T("./live/normal/480p-");

    LPCTSTR lpszRootFile = _T("./live/live.m3u8");
    LPCTSTR lpszLowFile = _T("./live/low/live.m3u8");
    LPCTSTR lpszNormalFile = _T("./live/normal/live.m3u8");
    LPCTSTR lpszHighFile = _T("./live/high/live.m3u8");

    if (!HLSProtocol_M3u8File_Create(&xhRoot, lpszRootFile))
    {
        printf("HLSProtocol_M3u8File_Create:%lX\n", HLSProtocol_GetLastError());
        return -1;
    }

    if (!HLSProtocol_M3u8File_AddStream(xhRoot, &xhLow, lpszLowFile, "www.xyry.org/live/low/live.m3u8", 150000))
    {
        printf("HLSProtocol_M3u8File_AddStream:%lX\n", HLSProtocol_GetLastError());
        return -1;
    }
    if (!HLSProtocol_M3u8File_AddStream(xhRoot, &xhNormal, lpszNormalFile, "www.xyry.org/live/normal/live.m3u8", 500000))
    {
        printf("HLSProtocol_M3u8File_AddStream:%lX\n", HLSProtocol_GetLastError());
        return -1;
    }
    if (!HLSProtocol_M3u8File_AddStream(xhRoot, &xhHigh, lpszHighFile, "www.xyry.org/live/high/live.m3u8", 2000000))
    {
        printf("HLSProtocol_M3u8File_AddStream:%lX\n", HLSProtocol_GetLastError());
        return -1;
    }

    if (!HLSProtocol_Section_Init(&xhToken, 12, HLSProtocol_CBNotify))
    {
        printf("HLSProtocol_Section_Init:%lX\n", HLSProtocol_GetLastError());
        return -1;
    }
    if (!HLSProtocol_Section_Input(xhToken, lpszVideoFile))
    {
        printf("HLSProtocol_Section_Input:%lX\n", HLSProtocol_GetLastError());
        return -1;
    }
    if (!HLSProtocol_Section_Output(xhToken, lpszDstFile))
    {
        printf("HLSProtocol_Section_Output:%lX\n", HLSProtocol_GetLastError());
        return -1;
    }
    if (!HLSProtocol_Section_Start(xhToken))
    {
        printf("HLSProtocol_Section_Start:%lX\n", HLSProtocol_GetLastError());
        return -1;
    }

    BOOL bRun = TRUE;
    while (bRun)
    {
        if (!HLSProtocol_Section_GetStatus(xhToken, &bRun))
        {
            break;
        }
    }
    HLSProtocol_Section_Close(xhToken);
    HLSProtocol_M3u8File_AddFile(xhRoot, xhNormal);
    //HLSProtocol_M3u8File_Delete(xhRoot, xhNormal);
    //HLSProtocol_M3u8File_Delete(xhRoot);
    printf("wandan\n");
    return 0;
}
